package algorithm.sort.array;

/**
 * @program: algorithm-data-structure
 * @description: bubbling sort method.
 * @author: Keyu Li
 * @create: 2021-08-30 09:51
 **/

public class SortBubble implements SortingMethod {
    String name = "Bubble";
    @Override
    public String getName(){
        return name;
    }
    @Override
    public void sort(int[] arr){
        if (arr.length <= 1)
            return;
        boolean isExchanged = false;
        int temp;
        for (int i = 0; i < arr.length; i++) {
            isExchanged = false;
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    isExchanged = true;
                }
            }
            if (!isExchanged)
                break;
        }
    }
}
